<?php

namespace McUser\Controller;
use Common\Controller\HomebaseController;

if(file_exists("Common/Conf/retJson.php")){
	$retJson=include "Common/Conf/retJson.php";
}else{
	$retJson=array();
}

class VerifyController extends HomebaseController {

	/*
	*	得到IP地址.
	*/
	function get_ip_1()
	{
	    if(!empty($_SERVER["HTTP_CLIENT_IP"]))
	    {
	        $cip = $_SERVER["HTTP_CLIENT_IP"];
	    }
	    else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
	    {
	        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	    }
	    else if(!empty($_SERVER["REMOTE_ADDR"]))
	    {
	        $cip = $_SERVER["REMOTE_ADDR"];
	    }
	    else
	    {
	        $cip = '';
	    }

	    preg_match("/[\d\.]{7,15}/", $cip, $cips);
	    $cip = isset($cips[0]) ? $cips[0] : 'unknown';
	    unset($cips);
	    return $cip;
	}

	function get_ip_2(){

		global $ip;
		if (getenv("HTTP_CLIENT_IP"))
			$ip = getenv("HTTP_CLIENT_IP");
		else if(getenv("HTTP_X_FORWARDED_FOR"))
			$ip = getenv("HTTP_X_FORWARDED_FOR");
		else if(getenv("REMOTE_ADDR"))
			$ip = getenv("REMOTE_ADDR");
		else $ip = "Unknow";

		return $ip;
	}

	 /*
	 *	发送一个短信验证码.
	 * @type: 	1,注册验证码; 
	 * 			2,重设密码验证码.
	 */
	public function getVerifyCode(){

		$mobile = I("post.mobile");
		$type = I("post.type");

		$code = C('retJson');

		$rejson = array(
					"retCode" => "1",
					"retMsg" => $code['1'] );

		if ($mobile == '' || $type != '1' && $type !='2'){
			/* 传入参数不正确 */
			$rejson['retCode'] = "-2";
			$rejson['retMsg'] = $code['-2'] ;
			$this -> ajaxReturn ( $rejson );
			return;
		}

		$his = D('VerifyHis');

		/*1, 检查手机号码是否已经注册 */
		$reg = $his -> check_mobile_registered($mobile);
		if ( $type =='1' && $reg){
			/* 1, 手机号码注册, 手机号码已经注册 */
			$rejson['retCode'] = '-104';
			$rejson['retMsg'] = $code['-104'] ;
			//$rejson['retMsg'] = 'mobile is registered';

			$this -> ajaxReturn($rejson,"json");
			return;
		}
		elseif ( $type =='2' && $reg == false){
			/* 2, 重设密码, 手机号码没有注册 */
			$rejson['retCode'] = '-110';
			$rejson['retMsg'] = $code['-110'] ;

			$this -> ajaxReturn($rejson);
			return;
		}
		
		/* 2, 同一个号码一天最多只能发3条短信 */		
		$hisNum = $his->check_mobile_num($mobile);

		/*
		echo 'mobile verify Num:'.$hisNum."<br/>";
		*/

		if ($hisNum >= 30){
			$rejson['retCode'] = '-109';
			$rejson['retMsg'] = $code['-109'] ;

			$this -> ajaxReturn($rejson);
			return;
		}

		/*3, 同一个IP每天只能发送3条短信 */
		$ip = $this -> get_ip_1();
		$ipNum = $his->check_ip_num($ip);
		if ($ipNum >= 30){
			$rejson['retCode'] = '-109';
			$rejson['retMsg'] = $code['-109'] ;

			$this -> ajaxReturn($rejson);
			return;
		}

		/*4, 生成随机码, 准备发送短信 */
		$verify_code = mt_rand(1000,9999);

		// 中文传不过去, 是乱码. 
		// $datas_sms = '【本果智富】验证码:' . $verify_code;
		// if ($type === '1'){
		// 	$datas_sms .= ', 感谢您注册本果财富管理,请于30分钟内输入.';
		// }else{
		// 	$datas_sms .= ', 感谢您注册本果财富管理,请于30分钟内输入并重设密码.';
		// }

		/*  -----------------------------------------------  发送短信验证码   ----------------------------------  */
		$my_sms = A('MySms');
		$re = $my_sms -> sendSMS($type, $mobile, $verify_code);
				
		if ( $re === '0' ){
			/* 保存短信验证码, 用来比对 */
			$his -> save_verify_code($mobile, $ip, $verify_code, $type);			
		} 
		else{
			/* 发送短信错误 */
			$rejson['retCode'] = $re;
			$rejson['retMsg'] = 'Send Verify SMS Error';	
		}
		
		/* 返回状态码 */
		$this->ajaxReturn($rejson);
		
	}
	
	/**
	 * 余额查询 用例
	 */
	public function getBalance()
	{
		$userID =  $_POST['userID'];
		$token =  $_POST['token'];

		$code = C('retJson');

		/* 数据参数 ticker 不能为空 */
		if ($userID == '' || $token == ''){
			$rejson = array(
				"retCode" => "-2",
				"retMsg" => $code['-2'] );
			$this->ajaxReturn($rejson);

			return;
		}
		/* 检查 token 是否正确 */ 
		$token_model = D('Token');
		$compare = $token_model -> compare_token_by_userID($userID, $token);
		if (! $compare){
			/* 手机号或者 Token 错误 */
			$rejson['retCode'] = "-111";
			$rejson['retMsg'] = $code['-111'];
			$this -> ajaxReturn ($rejson) ;		
			return;
		}

		$my_sms = A('MySms');
		$balance = $my_sms->getBalance();
		
		//echo APP_PATH .'Common/Conf/retJson.php';
		//echo $retJson;
		//$retJson = require( APP_PATH .'Common/Conf/retJson.php');
		$code = C('retJson');

		$rejson['retCode'] = '1';
		$rejson['retMsg'] = $code['1'] ;
		$rejson['data'] = array(
			'balance' => $balance
		);

		/* 返回状态码 */
		$this -> ajaxReturn($rejson);
	}


}